import { UserI } from "@/models/user";
import $api from "@/api/index";
import { SpecializationI } from "@/models/specialization";
import { APIPagination } from "@/models/common";
import { useCallback, useEffect, useState } from "react";
import { getUsers } from "@/api/users";
import { GroupI } from "@/models/group";

export async function getGroup(
  params?: URLSearchParams | null
): Promise<APIPagination<GroupI>> {
  const res = await $api.get("api/group/");

  return res.data;
}

export function useGroup(
  params?: URLSearchParams | null
): [APIPagination<GroupI> | undefined, GroupI[] | undefined, boolean] {
  const [isLoading, setLoading] = useState(false);
  const [data, setData] = useState<APIPagination<GroupI>>();

  const load = useCallback(async () => {
    // if (!params) return;
    setLoading(true);

    try {
      const r = await getGroup(params);
      setData(r);
    } catch (error) {
      console.log({ error });
    } finally {
      setLoading(false);
    }
  }, [params]);

  useEffect(() => {
    load();
  }, [load]);

  return [data, data?.results, isLoading];
}
